//
//  EMGeeBaseViewController.m
//  PictureSelector
//
//  Created by Lyy on 15/8/24.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

#define kBarHiddenDefayTime (3)  //隐藏视频窗口barView的延迟时间 ：秒

#import "EMGeeBaseViewController.h"
#import "Extra.h"

@interface EMGeeBaseViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
BottomBarViewDelegate,
TopBarViewDelegate,
UIGestureRecognizerDelegate
>

@end

@implementation EMGeeBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)createView {
    self.videoView = [[UIView alloc] initWithFrame:CGRectZero];
    self.videoView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    [self.view addSubview:self.videoView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(videoViewHidden:)];
    tap.delegate = self;
    [self.videoView addGestureRecognizer:tap];
    
    self.bottomBarView = [[BottomBarView alloc] initWithFrame:CGRectZero];
    self.bottomBarView.delegate = self;
    [self.videoView addSubview:self.bottomBarView];
    
    self.topBarView = [[[NSBundle mainBundle] loadNibNamed:@"EMTopBarView" owner:self options:nil] lastObject];
    self.topBarView.delegate = self;
    [self.videoView addSubview:self.topBarView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

#pragma mark - tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    
    return cell;
}

#pragma mark - update frame
- (void)videoViewHidden:(UIGestureRecognizer *)tap {
    self.barViewHidden = !self.barViewHidden;
    if (self.barViewHidden) {
        [UIView animateWithDuration:kAnimationTime animations:^{
            self.bottomBarView.transform = CGAffineTransformMakeTranslation(0,kBarViewHeight);
            self.topBarView.transform = CGAffineTransformMakeTranslation(0, -kTopViewHeight);
        } completion:^(BOOL finished) {
        }];
    } else {
        
        [UIView animateWithDuration:kAnimationTime animations:^{
            self.bottomBarView.transform = CGAffineTransformIdentity;
            self.topBarView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }
//    如果视频窗口上barView状态为显示，则延迟3s后，设置为隐藏状态
    if (self.barViewHidden) {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hiddenBarView) object:nil];
        [self performSelector:@selector(hiddenBarView) withObject:nil afterDelay:kBarHiddenDefayTime];
    } else {
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hiddenBarView) object:nil];
    }
}

- (void)hiddenBarView {
    
    [self videoViewHidden:nil];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    CGPoint point = [gestureRecognizer locationInView:self.videoView];
    CGPoint bottomBarViewPoint = self.bottomBarView.frame.origin;
    CGRect TopBarViewRect = self.topBarView.frame;
    
    if (point.y > bottomBarViewPoint.y) {
        return NO;
    } else if (point.y < CGRectGetMaxY(TopBarViewRect)) {
        return NO;
    } else {
        return YES;
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat bottomBarView_originY = 0;
    
    if (MSIsLandscape()) {
        
        [self.videoView setFrame:CGRectMake(0, bottomBarView_originY, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
        
    } else {
        [self.videoView setFrame:CGRectMake(0,bottomBarView_originY, CGRectGetWidth(self.view.frame), kDefaultHeight)];
        
    }
    
    [self.bottomBarView setFrame:CGRectMake(0, CGRectGetHeight(self.videoView.frame)-kBarViewHeight, CGRectGetWidth(self.videoView.frame), kBarViewHeight)];
    self.topBarView.frame = CGRectMake(0, 0, CGRectGetWidth(self.videoView.frame), kTopViewHeight);
    
    if (self.barViewHidden) {
        [self videoViewHidden:nil];
    }
    
    [self.tableView setFrame:CGRectMake(0, CGRectGetMaxY(self.videoView.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-CGRectGetMaxY(self.videoView.frame))];
}

#pragma mark -
#pragma mark - TopBarViewDelegate

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)playMode {
    //    音视频模式切换
}

#pragma mark -
#pragma mark - BottomBarViewDelegate

- (void)bottomBarView:(BottomBarView *)view isPlaying:(BOOL)isPlaying {
    
}

- (void)bottomBarView:(BottomBarView *)view slidePosition:(CGFloat)currentPosition {
    
}

- (void)bottomBarViewFullScreen {
    
    //强制旋转
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIDeviceOrientationPortrait] forKey:@"orientation"];
        
    } else {
        
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight ] forKey:@"orientation"];
    }
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
